********************************************************************************
* Input file: data_ind_X_2017
* Output file: One row per coworker, with assigned peer group (WP_HPG_id)
* Description: Assign each individual to a workplace × HPG_Coworkers peer group.
********************************************************************************

use Data/data_ind_X_2017, clear

* Drop people with missing or invalid workplaces
drop if missing(WorkplaceId)

* Restrict to workplaces with at least 2 employees
keep if num_of_workers >= 2

* Construct homogeneous peer group within workplace
* Global $HPG_Coworkers is defined in the main do-file (e.g., "EduCollege Female")
di "Using global HPG_Coworkers grouping: $HPG_Coworkers"
assert "$HPG_Coworkers" != ""

egen WP_HPG_type = group($HPG_Coworkers)
* With 1000 we can have as many as 1000 HPG_Coworkers_type:s (won't happen)
gen double WP_HPG_id = WorkplaceId * 1000 + WP_HPG_type

* Keep only the key variables needed for downstream collapse
keep PersonId WorkplaceId WP_HPG_type WP_HPG_id num_of_workers $HPG_Coworkers

order WorkplaceId PersonId num_of_workers
compress
save Data/data_coworkers, replace


